import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

#读取图片
img = cv.imread('../test_datas/sudoku.png')
rows,cols,ch = img.shape

# 原图的四个点
pts1 = np.float32([[73,87],
                   [490,71],
                   [36,515],
                   [520,520]])
# 目标四个点
pts2 = np.float32([[0,0],
                   [300,0],
                   [0,300],
                   [300,300]])
# 计算调整值
M = cv.getPerspectiveTransform(pts1,pts2)
print(M)
dst = cv.warpPerspective(img,M,(300,300))

plt.figure(figsize=(15,15))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

# 二次变换
pts1 = np.float32([[354,360],[510,356],[356,515],[520,520]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))

plt.figure(figsize=(15,15))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()